/*
 * @Autor: taobo
 * @Date: 2020-09-23 23:14:49
 */
#include "lock_free_queue.h"

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <cstdint>
#include <thread>
using namespace std;

static ofstream ofs("objs/result.txt",std::ofstream::out);

void produce_func(LockFreeQueue* queue, uint32_t num) {
  for (uint32_t i = 0; i < num; ++i) {
    char buf[10];
    sprintf(buf, "%04u", i);
    while (queue->put((uint8_t*)buf, 5) == false) {
      ;
    }
    ofs << "produce --> " << buf << '\n';
    // fflush(stdout);
  }
}

void consume_func(LockFreeQueue* queue, uint32_t num) {
  char buf[10];
  for (uint32_t i = 0; i < num; ++i) {
    while (queue->get((uint8_t*)buf, 5) == false) {
      ;
    }
    ofs << "consume --> " << buf << ',' << i << '\n';
  }
}

int main() {
  
  LockFreeQueue* queue = new LockFreeQueue(10);
  ofs << queue->size() << '\n';
  thread produce(produce_func, queue, 100);
  thread consume(consume_func, queue, 100);
  produce.join();
  consume.join();
  delete queue;
  return 0;
}
